/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class BattleScreen extends Activity {

	private ArrayList<Character> characterList = new ArrayList<Character>();

	Character character;

	int displayMode;
	int returnHealth;
	boolean initMagicInitialized;
	boolean characterUpdateNeeded;

	Spell tempSpell;

	MenuInflater menuInflator;

	//////////////////////////////////
	//This block contains all element initializations for the battle Screen_magic view
	//

	////////Level up dialog
	LinearLayout experienceLayout;
	SeekBar levelupSeekBar;
	TextView levelupHitDieTxt;
	TextView levelupAmountTxt;
	Button levelupButton;
	////////

	////////Battle_screen_magic

	LinearLayout swipeLayout;

	LinearLayout healthBoxLayout;
	LinearLayout healthBarLayout;

	ListAdapter spellList;

	ProgressBar healthbar;
	ProgressBar experienceBar;

	TextView currentHealthTxt;//battleMagicHealthCurrent;
	TextView totalHealthTxt;//battleMagicHealthCurrent;

	//Ability Score TextView IDs
	TextView strTxt; //battlemagicAbilStr
	TextView dexTxt; //battlemagicAbilDex
	TextView conTxt; //battlemagicAbilCon
	TextView intTxt; //battlemagicAbilInt
	TextView wisTxt; //battlemagicAbilWis
	TextView chaTxt; //battlemagicAbilCha

	TextView strModTxt; //battlemagicAbilStrMod
	TextView dexModTxt; //battlemagicAbilDexMod
	TextView conModTxt; //battlemagicAbilConMod
	TextView intModTxt; //battlemagicAbilIntMod
	TextView wisModTxt; //battlemagicAbilWisMod
	TextView chaModTxt; //battlemagicAbilChaMod

	TextView conciousWarnTxt;//battlemagicConciousState

	TextView armorClassTxt; //battlemagicarmorClass

	TextView saveFortTxt; //battleagicFortSave
	TextView saveReflexTxt; //battlemagicRefSave
	TextView saveWillTxt; //battlemagicWillSave

	TextView experienceTxt;//battlemagicExpCurrent
	TextView baseExpTxt;//battlemagicExplevelBase
	TextView maxExpTxt;//battlemagicExplevelMax
	////////

	//
	//
	//////////////////////////////////

	//////////////////////////////////
	//This block contains all element initializations for the magic_spells_menu view
	//

	//This variable is used to determine which spell book to load when the user swipes their finger to switch views.
	//It helps swipe from spellbook to spellbook, other to Spellbook, and spellbook to other..
	int currentSpellBookView;

	Spinner classSelectionSpinner;
	SpellExpandableListAdapter spellListAdapter;
	ExpandableListView list;
	ExpandableListView fillerList;
	LayoutInflater inflater;
	AutoCompleteTextView acTextView;
	LinearLayout magicLinearLayout;

	///////Objects for Add Spell dialog
	Button addSpellDoneButton;
	TextView spellInformaion;

	//
	//
	//////////////////////////////////

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent data = getIntent();

		Bundle myResults = data.getExtras();

		character = (Character) myResults.get("character");

		//TODO: Warning: This method may not work for tablets. They do not call the onCreateOptionsMenu when 'Menu' is pressed.
		displayMode = ConstantHelper.DISPLAY_MODE_MAGIC_NORMAL;
		setContentView(R.layout.battle_screen_magic);
		initBattleScreen();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		switch (displayMode) {
		case 1:
			menu.clear();
			menu.add(0, ConstantHelper.MENU_BUTTON_WEAPONS, 0, "Weapons").setIcon(R.drawable.plus);
			menu.add(0, ConstantHelper.MENU_BUTTON_SPELLS, 0, "Spells").setIcon(R.drawable.minus);
			break;
		case 4:
			menu.clear();
			if (character.characterClass.getCharacterMagicType(currentSpellBookView) == ConstantHelper.CHAR_TYPE_SPECIAL_MAGIC) {
				menu.add(0, ConstantHelper.MENU_BUTTON_ADD_SPELL, 0, "Add Spell").setIcon(R.drawable.plus);
				menu.add(0, ConstantHelper.MENU_BUTTON_RESET_SPELLS, 0, "Reset Spells Count").setIcon(R.drawable.plus);
			} else { // will be normal magic type
				menu.add(0, ConstantHelper.MENU_BUTTON_ADD_SPELL, 0, "Add Spell").setIcon(R.drawable.plus);
				menu.add(0, ConstantHelper.MENU_BUTTON_PREPARE_SPELLS, 0, "Prepare Spells").setIcon(R.drawable.plus);
				menu.add(0, ConstantHelper.MENU_BUTTON_RESET_SPELLS, 0, "Reset Spells Count").setIcon(R.drawable.plus);
			}
			break;
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ConstantHelper.MENU_BUTTON_RESET_SPELLS:

			character.characterClass.resetSpellsRemainingToMax(currentSpellBookView);

			spellListAdapter.setSpellArrays();
			return true;
		case ConstantHelper.MENU_BUTTON_WEAPONS:

			finish();
			return true;

		case ConstantHelper.MENU_BUTTON_PREPARE_SPELLS:

			Bundle extras = new Bundle();
			extras.putSerializable("character", character);
			extras.putInt("currentView", currentSpellBookView);

			Intent i = new Intent(BattleScreen.this, PrepareSpells.class);
			i.putExtras(extras);
			startActivityForResult(i, ConstantHelper.REQUEST_CODE_PREPARE_SPELLS);
			return true;

		case ConstantHelper.MENU_BUTTON_ADD_SPELL:

			final Dialog dialog = new Dialog(BattleScreen.this);
			dialog.setContentView(R.layout.spell_selection_dialog);
			dialog.setTitle("Add a Spell");
			dialog.setCancelable(true);

			int[] spellsSlotsAvailable = character.characterClass.getFreeSpellSlotsSpecial(currentSpellBookView);

			//This function gets all the spells that the user can select from based off the number of free slots available to them at their
			//current level.
			String[] spellStringArrayList = ConstantSpellHelper.concatinateSpellLists(spellsSlotsAvailable, ConstantHelper.classIntToString(currentSpellBookView),
					character.characterClass.getMaxSpellLevelForSpecificClass(currentSpellBookView));

			//The adapter that recieves the layout type from android and the array creatd by the above function.
			//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, spellStringArray);
			//final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, spellStringArrayList);
			final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, spellStringArrayList);
			//Find the view blah blah blah...
			acTextView = (AutoCompleteTextView) dialog.findViewById(R.id.spellSelectionAutoCompleteTxt);
			addSpellDoneButton = (Button) dialog.findViewById(R.id.spellSelectionAddButton);
			spellInformaion = (TextView) dialog.findViewById(R.id.spellSelectionInformationTxt);

			addSpellDoneButton.setEnabled(false);

			//The amount of letters is needs before it starts auto completing
			acTextView.setThreshold(1);

			//Set that adapter!
			acTextView.setAdapter(adapter);

			acTextView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					String itemName = adapter.getItem(position);
					FileHelper fh = new FileHelper();
					Spell spell = fh.getSpellInformationBasedOnName(itemName, BattleScreen.this, currentSpellBookView);

					StringBuffer sb = new StringBuffer("");
					for (int i = 0; i < spell.getSpellProperties().size(); i++) {
						sb.append(spell.getConcatinatedHtmlProperty(i));
					}
					spellInformaion.setText(Html.fromHtml(sb.toString()));
					tempSpell = spell;
					addSpellDoneButton.setEnabled(true);
					//acTextView.setEnabled(false);
					acTextView.setText("");
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(spellInformaion.getWindowToken(), 0);
				}
			});

			addSpellDoneButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					character.spells[currentSpellBookView].add(tempSpell);
					spellListAdapter.setSpellArrays();
					dialog.cancel();
				}
			});

			//			acTextView.setOnTouchListener(new OnTouchListener() {
			//				
			//				@Override
			//				public boolean onTouch(View v, MotionEvent event) {
			//					// TODO Auto-generated method stub
			//					return false;
			//				}
			//			});

			dialog.show();

			return true;
		}
		return false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		FileHelper fh = new FileHelper();

		//Read in old list1
		characterList = fh.returnCharactersFromFile(ConstantHelper.FILENAME);

		//Create new List2
		ArrayList<Character> newChList = new ArrayList<Character>();

		//TODO: Unique IDs are not matching up!

		if (characterList.size() == 0) {
			newChList.add(character);
			//Write to file
			fh.writeCharacters(ConstantHelper.FILENAME, newChList);
		} else {
			for (Character ch : characterList) {
				if (ch.equals(character))
					newChList.add(character);
				else
					newChList.add(ch);
			}
			fh.writeCharacters(ConstantHelper.FILENAME, newChList);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		try {
			if (requestCode == ConstantHelper.REQUEST_CODE_PREPARE_SPELLS) {
				Bundle myResults = data.getExtras();

				Character tempCharacter = (Character) myResults.get("character");

				this.character = tempCharacter;

				initSpells(currentSpellBookView);

			}
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "Data was not read Successfully", Toast.LENGTH_LONG).show();
		}
	}

	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//Beginning of Main_BattleScreen_magic menu code
	//
	//
	//

	//This view is initialized if the character's magic type is normal or special
	/**
	 * 
	 */
	private void initBattleScreen() {

		//		if (character.characterClass.getCharacterMagicType() == ConstantHelper.CHAR_TYPE_NORMAL_MAGIC || character.info.getCharcterMagicType() == ConstantHelper.CHAR_TYPE_SPECIAL_MAGIC) {
		//
		//			//TODO: Warning: This method may not work for tablets. They do not call the onCreateOptionsMenu when 'Menu' is pressed.
		//			displayMode = ConstantHelper.DISPLAY_MODE_MAGIC_NORMAL;
		//			setContentView(R.layout.battle_screen_magic);
		//		}

		setContentView(R.layout.battle_screen_magic);
		displayMode = ConstantHelper.DISPLAY_MODE_MAGIC_NORMAL;

		experienceLayout = (LinearLayout) findViewById(R.id.LinearLayoutExperience);
		healthBoxLayout = (LinearLayout) findViewById(R.id.healthBoxLinearLayout);
		healthBarLayout = (LinearLayout) findViewById(R.id.healthBarLinearLayout);
		swipeLayout = (LinearLayout) findViewById(R.id.mainSwipeLayout);

		currentHealthTxt = (TextView) findViewById(R.id.battleMagicHealthCurrent);
		totalHealthTxt = (TextView) findViewById(R.id.battleMagicHealthTotal);

		strTxt = (TextView) findViewById(R.id.battlemagicAbilStr);
		dexTxt = (TextView) findViewById(R.id.battlemagicAbilDex);
		conTxt = (TextView) findViewById(R.id.battlemagicAbilCon);
		intTxt = (TextView) findViewById(R.id.battlemagicAbilInt);
		wisTxt = (TextView) findViewById(R.id.battlemagicAbilWis);
		chaTxt = (TextView) findViewById(R.id.battlemagicAbilCha);

		strModTxt = (TextView) findViewById(R.id.battlemagicAbilStrMod);
		dexModTxt = (TextView) findViewById(R.id.battlemagicAbilDexMod);
		conModTxt = (TextView) findViewById(R.id.battlemagicAbilConMod);
		intModTxt = (TextView) findViewById(R.id.battlemagicAbilIntMod);
		wisModTxt = (TextView) findViewById(R.id.battlemagicAbilWisMod);
		chaModTxt = (TextView) findViewById(R.id.battlemagicAbilChaMod);

		armorClassTxt = (TextView) findViewById(R.id.battlemagicArmorClass);

		saveFortTxt = (TextView) findViewById(R.id.battleagicFortSave);
		saveReflexTxt = (TextView) findViewById(R.id.battlemagicRefSave);
		saveWillTxt = (TextView) findViewById(R.id.battlemagicWillSave);

		conciousWarnTxt = (TextView) findViewById(R.id.battlemagicConciousState);

		experienceTxt = (TextView) findViewById(R.id.battlemagicExpCurrent);

		healthbar = (ProgressBar) findViewById(R.id.battlemagicHealthBar);

		experienceTxt = (TextView) findViewById(R.id.battlemagicExpCurrent);//battlemagicExpCurrent
		baseExpTxt = (TextView) findViewById(R.id.battlemagicExplevelBase);//battlemagicExplevelBase
		maxExpTxt = (TextView) findViewById(R.id.battlemagicExplevelMax);//battlemagicExplevelMax
		experienceBar = (ProgressBar) findViewById(R.id.battlemagicExpBar);

		//Updates all the elements on the battleScreen
		updateBattleScreenMagic();

		experienceLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				final Dialog dialog = new Dialog(BattleScreen.this);
				dialog.setContentView(R.layout.level_up_dialog);
				dialog.setTitle("Add Experience");
				dialog.setCancelable(true);

				final EditText experienceEditText = (EditText) dialog.findViewById(R.id.expEditTxt);
				final Button button1 = (Button) dialog.findViewById(R.id.expButton1);
				final Button button2 = (Button) dialog.findViewById(R.id.expButton2);
				final Button button3 = (Button) dialog.findViewById(R.id.expButton3);
				final Button button4 = (Button) dialog.findViewById(R.id.expButton4);
				final Button button5 = (Button) dialog.findViewById(R.id.expButton5);
				final Button button6 = (Button) dialog.findViewById(R.id.expButton6);
				final Button button7 = (Button) dialog.findViewById(R.id.expButton7);
				final Button button8 = (Button) dialog.findViewById(R.id.expButton8);
				final Button button9 = (Button) dialog.findViewById(R.id.expButton9);
				final Button button0 = (Button) dialog.findViewById(R.id.expButton0);
				final Button buttonBack = (Button) dialog.findViewById(R.id.expButtonBack);
				final Button buttonClear = (Button) dialog.findViewById(R.id.expButtonClear);
				final Button buttonSubmit = (Button) dialog.findViewById(R.id.expButtonSubmit);

				buttonSubmit.setEnabled(false);

				experienceEditText.setText("");

				button1.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("1");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("2");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button3.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("3");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button4.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("4");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button5.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("5");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button6.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("6");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button7.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("7");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button8.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("8");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button9.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("9");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				button0.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						experienceEditText.append("0");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				buttonBack.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						//Removes one character off the end of the string
						String newStr = experienceEditText.getText().toString();
						if (newStr.length() > 0) {
							newStr = newStr.substring(0, newStr.length() - 1);
							//Set the text to the new string
							experienceEditText.setText(newStr);
						}
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				buttonClear.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						//Removes all the text
						experienceEditText.setText("");
						if (experienceEditText.getText().length() == 0)
							buttonSubmit.setEnabled(false);
						else
							buttonSubmit.setEnabled(true);
					}
				});
				buttonSubmit.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						//This adds to the character's experience points
						character.info.addToExperiencePoints(Integer.parseInt(experienceEditText.getText().toString()));

						//Cancel the dialog and check if the character needs leveling up
						dialog.cancel();

						//In either case, the screen will be updated. 
						//Either through the levelUpCharacter function or through theupdate BattleScreen Function
						if (character.info.getExperience() > character.info.getExpToNextLevel(character.characterClass.getLevel()))
							levelUpCharacter();
						else
							updateBattleScreenMagic();
					}

				});

				dialog.show();
				return false;
			}
		});

		healthBoxLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				int curHealth = character.info.getCurrentHP();
				int maxHealth = character.info.getTotalHP();

				final int maxNum = maxHealth - curHealth;
				final int minNum = Math.abs(curHealth - (-10));

				final Dialog dialog = new Dialog(BattleScreen.this);
				dialog.setContentView(R.layout.health_adjust_dialog);
				dialog.setTitle("Adjust your health");
				dialog.setCancelable(true);

				final EditText healthEditText = (EditText) dialog.findViewById(R.id.healthadjustEditTxt);
				final Button button1 = (Button) dialog.findViewById(R.id.healthadjustButton1);
				final Button button2 = (Button) dialog.findViewById(R.id.healthadjustButton2);
				final Button button3 = (Button) dialog.findViewById(R.id.healthadjustButton3);
				final Button button4 = (Button) dialog.findViewById(R.id.healthadjustButton4);
				final Button button5 = (Button) dialog.findViewById(R.id.healthadjustButton5);
				final Button button6 = (Button) dialog.findViewById(R.id.healthadjustButton6);
				final Button button7 = (Button) dialog.findViewById(R.id.healthadjustButton7);
				final Button button8 = (Button) dialog.findViewById(R.id.healthadjustButton8);
				final Button button9 = (Button) dialog.findViewById(R.id.healthadjustButton9);
				final Button button0 = (Button) dialog.findViewById(R.id.healthadjustButton0);
				final Button buttonBack = (Button) dialog.findViewById(R.id.healthadjustButtonBack);
				final Button buttonClear = (Button) dialog.findViewById(R.id.healthadjustButtonClear);

				final ImageButton plusButton = (ImageButton) dialog.findViewById(R.id.healthadjustPlus);
				final ImageButton minusButton = (ImageButton) dialog.findViewById(R.id.healthadjustMinus);

				healthEditText.setText("");
				healthEditText.setFocusable(false);

				button1.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("1");
					}
				});
				button2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("2");
					}
				});
				button3.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("3");
					}
				});
				button4.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("4");
					}
				});
				button5.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("5");
					}
				});
				button6.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("6");
					}
				});
				button7.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("7");
					}
				});
				button8.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("8");
					}
				});
				button9.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("9");
					}
				});
				button0.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						healthEditText.append("0");
					}
				});
				buttonBack.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						//Removes one character off the end of the string
						String newStr = healthEditText.getText().toString();
						if (newStr.length() > 0) {
							newStr = newStr.substring(0, newStr.length() - 1);
							//Set the text to the new string
							healthEditText.setText(newStr);
						}
					}
				});
				buttonClear.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						//Removes all the text
						healthEditText.setText("");
					}
				});

				//Increases the current health of the player by the amount entered by the user on the dialog box.
				plusButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						try {
							if (Integer.parseInt(healthEditText.getText().toString()) <= maxNum && healthEditText.getText().length() > 0) {
								int healthChange = Integer.parseInt(healthEditText.getText().toString());
								character.info.adjustHealth(healthChange);
								dialog.cancel();
								updateBattleScreenMagic();
							} else {
								Toast.makeText(BattleScreen.this, "Invalid Entry made", Toast.LENGTH_LONG);
							}
						} catch (NumberFormatException e) {
							Toast.makeText(BattleScreen.this, "No number entered", Toast.LENGTH_LONG);
						}

					}
				});

				//On long Click resets the player's health to full.
				plusButton.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						character.info.setCurrentHP(character.info.getTotalHP());
						dialog.cancel();
						updateBattleScreenMagic();
						return true;
					}
				});

				//Decreases the current health of the player by the amount entered by the user on the dialog box.
				minusButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						try {
							if (Integer.parseInt(healthEditText.getText().toString()) <= minNum && healthEditText.getText().length() > 0) {
								int healthChange = Integer.parseInt(healthEditText.getText().toString());
								character.info.adjustHealth(-(healthChange));
								dialog.cancel();
								updateBattleScreenMagic();
							} else {
								Toast.makeText(BattleScreen.this, "Invalid Entry made", Toast.LENGTH_LONG);
							}
						} catch (NumberFormatException e) {
							Toast.makeText(BattleScreen.this, "No number entered", Toast.LENGTH_LONG);
						}

					}
				});

				dialog.show();
			}
		});

		//Click listener is needed here for some odd reason so it will detect the onFlingGesture listener
		healthbar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("CHAR", "CKICKING HEALTH");

			}
		});

		//So you can swipe the health bar to increase or decrease it
		healthbar.setOnTouchListener(new OnFlingGestureListener() {

			@Override
			public void onTopToBottom() {
			}

			//When you swipe left, it will decrease the health of the character
			@Override
			public void onRightToLeft() {
				if (character.info.getCurrentHP() > -10) {
					character.info.setCurrentHP(character.info.getCurrentHP() - 1);

					updateProgressBars();
					currentHealthTxt.setText(String.valueOf(character.info.getCurrentHP()));
					Log.d("CHAR", "FLING LEFT");

				}
			}

			//When you swipe right, it will increase the health of the character
			@Override
			public void onLeftToRight() {
				if (character.info.getCurrentHP() < character.info.getTotalHP()) {
					character.info.setCurrentHP(character.info.getCurrentHP() + 1);
					updateProgressBars();
					currentHealthTxt.setText(String.valueOf(character.info.getCurrentHP()));
					Log.d("CHAR", "FLING RIGHT");
				}
			}

			@Override
			public void onBottomToTop() {
			}
		});

		//Swipe click listener for the same reason as the healthbar click listener.... Weird
		swipeLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});

		//The onFling listener for the main layout of the BattleScreen. Switches to spells and Items for right nad left swiping
		swipeLayout.setOnTouchListener(new OnFlingGestureListener() {

			@Override
			public void onTopToBottom() {
			}

			@Override
			public void onRightToLeft() {
				Log.d("CHAR", "SWITCH TO SPELLS");
				if (character.characterClass.characterIsMagical()) {
					int nextBook = character.characterClass.getNextSpellBookLowToHigh(0, character.characterClass.getCharacterClassLevelsArray());
					initSpells(nextBook);
				} else
					;
				//initWeapons();

			}

			@Override
			public void onLeftToRight() {
			}

			@Override
			public void onBottomToTop() {
			}
		});

	}

	/**
	 * This function will initiate the level up process:
	 * 
	 * 1: Notify the user they have leveled up
	 * 
	 * 2: Ask them how much health to add to their chracter's current health
	 * total
	 * 
	 * 3: call the Character.levelUp function
	 * 
	 */
	public void levelUpCharacter() {

		//This dialog will get the character's health increase
		final Dialog dialog = new Dialog(BattleScreen.this);
		dialog.setContentView(R.layout.level_up_health);
		dialog.setTitle("Level up!");
		dialog.setCancelable(false);

		//Bind some variables to the views in the dialog

		levelupSeekBar = (SeekBar) dialog.findViewById(R.id.levelupSeekBar);
		levelupAmountTxt = (TextView) dialog.findViewById(R.id.levelupAmountTxt);
		levelupHitDieTxt = (TextView) dialog.findViewById(R.id.levelupHitDieTxt);
		levelupButton = (Button) dialog.findViewById(R.id.levelUpSubmitButton);

		//Find the hit die and assign it to the text for the user's convenience
		int hitDie = character.characterClass.getHitDieForClass(character.characterClass.getBeginningClass());

		levelupHitDieTxt.setText("Hit die: d" + String.valueOf(hitDie));
		levelupAmountTxt.setText("1");

		//Set the max the bar can go (hit die)
		levelupSeekBar.setMax(hitDie);

		//Initialize at 1
		levelupSeekBar.setProgress(1);
		levelupSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			//When the progress changes (drags the slider), update the text view
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				levelupSeekBar.setProgress(progress);
				levelupAmountTxt.setText(String.valueOf(progress));

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		levelupButton.setOnClickListener(new OnClickListener() {

			//When a health to increase is determined and the button is clicked, Add it to the character's health
			//And initiate the levelup function for the character.
			@Override
			public void onClick(View v) {
				int healthAmt = Integer.parseInt(levelupAmountTxt.getText().toString());
				returnHealth = healthAmt;
				character.levelUp(returnHealth, character.characterClass.getBeginningClass());
				dialog.cancel();
				updateBattleScreenMagic();
			}
		});

		dialog.show();
	}

	/**
	 * Updates the battleScreen for magic user.
	 */
	private void updateBattleScreenMagic() {

		//Sets all the text views
		strTxt.setText(String.valueOf(character.abilities.getSTR()));
		dexTxt.setText(String.valueOf(character.abilities.getDEX()));
		conTxt.setText(String.valueOf(character.abilities.getCON()));
		intTxt.setText(String.valueOf(character.abilities.getINT()));
		wisTxt.setText(String.valueOf(character.abilities.getWIS()));
		chaTxt.setText(String.valueOf(character.abilities.getCHA()));

		strModTxt.setText(getModSign(character.abilities.getSTRMOD()));
		dexModTxt.setText(getModSign(character.abilities.getDEXMOD()));
		conModTxt.setText(getModSign(character.abilities.getCONMOD()));
		intModTxt.setText(getModSign(character.abilities.getINTMOD()));
		wisModTxt.setText(getModSign(character.abilities.getWISMOD()));
		chaModTxt.setText(getModSign(character.abilities.getCHAMOD()));

		armorClassTxt.setText(String.valueOf(character.info.getArmorClass()));

		saveFortTxt.setText(getModSign(character.characterClass.getSavingThrowFortitude()));
		saveReflexTxt.setText(getModSign(character.characterClass.getSavingThrowReflex()));
		saveWillTxt.setText(getModSign(character.characterClass.getSavingThrowWill()));

		currentHealthTxt.setText(String.valueOf(character.info.getCurrentHP()));
		totalHealthTxt.setText(String.valueOf(character.info.getTotalHP()));

		updateProgressBars();
	}

	/**
	 * Gets the appropriate character '+' or '-' depending on the number
	 * 
	 * @param modifier
	 * @return
	 */
	private String getModSign(int modifier) {
		String mod = String.valueOf(modifier);
		String retStr;

		if (modifier > 0)
			retStr = "+" + mod;
		else if (modifier == 0)
			retStr = "  " + mod;
		else
			retStr = "- " + Math.abs(Integer.valueOf(mod));
		return retStr;
	}

	public void updateProgressBars() {

		//Gets the base score for the level the character is currently at
		int levelBase = character.info.getExpToNextLevel(character.characterClass.getLevel() - 1);

		//Gets the current experience of the character 
		int currentExp = character.info.getExperience();

		//Gets the absolute point total needed to level up.
		int nextLevelPointTotal = character.info.getExpToNextLevel(character.characterClass.getLevel());

		if (character.info.getConciousState() == 2)
			conciousWarnTxt.setText("");
		else if (character.info.getConciousState() == 0)
			conciousWarnTxt.setText("DEAD");
		else
			conciousWarnTxt.setText("UNCONCIOUS");

		experienceTxt.setText(String.valueOf(currentExp));
		baseExpTxt.setText(String.valueOf(levelBase));
		maxExpTxt.setText(String.valueOf(nextLevelPointTotal));

		//Set up the progress bar to reflect the changes
		experienceBar.setMax(nextLevelPointTotal - levelBase);
		healthbar.setMax(character.info.getTotalHP());
		healthbar.setVisibility(View.VISIBLE);
		experienceBar.setVisibility(View.VISIBLE);

		healthbar.setProgress(character.info.getCurrentHP());
		experienceBar.setProgress(currentExp - levelBase);
	}

	//
	//
	//
	//End of Main_BattleScreen_magic menu code
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//

	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//Beginning of magic_spells_menu code
	//
	//
	//

	private void initSpells(int spellBookPosition) {

		currentSpellBookView = spellBookPosition;

		//
		magicLinearLayout = (LinearLayout) findViewById(R.id.spellLinearLayout);

		//This adapter handles the expandable list that is displayed
		spellListAdapter = new SpellExpandableListAdapter();

		//This function gets the spells for the character and properly arranges them to be displayed in the dropdown list
		spellListAdapter.setSpellArrays();

		//Change the display mode to make the currect menu be inflated 
		displayMode = ConstantHelper.DISPLAY_MODE_SPELLS;

		//Set the content view to the spells menu
		setContentView(R.layout.magic_spells_menu);

		//Set list to the ExpandableListView
		list = (ExpandableListView) findViewById(R.id.list);
		fillerList = (ExpandableListView) findViewById(R.id.fillerExpandableList);

		//Sets adapter..??
		list.setAdapter(spellListAdapter);

		//Removes the dropdown icon
		list.setGroupIndicator(null);

		//This for loop espands the group elements the user had expanded when
		//they left the spell view to visit another. 
		for (int i = 0; i < spellListAdapter.getGroupCount(); i++) {
			if (character.info.getGroupIsExpandedElement(i) == 1)
				list.expandGroup(i);
		}

		//Long clicking the group
		list.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Log.d("CHAR", "LONG GROUP");
				return true;
			}
		});

		fillerList.setOnTouchListener(new OnFlingGestureListener() {

			@Override
			public void onTopToBottom() {
			}

			@Override
			public void onRightToLeft() {
				//Redraws the magic menu
				int spellScreen = character.characterClass.getNextSpellBookLowToHigh(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
				if (spellScreen != -1)
					initSpells(spellScreen);
				else
					initBattleScreen();
			}

			@Override
			public void onLeftToRight() {
				//Redraws the magic menu
				int spellScreen = character.characterClass.getNextSpellBookHighToLow(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
				if (spellScreen != -1)
					initSpells(spellScreen);
				else
					initBattleScreen();
			}

			@Override
			public void onBottomToTop() {
			}
		});

		list.setOnTouchListener(new OnFlingGestureListener() {

			@Override
			public void onTopToBottom() {
			}

			@Override
			public void onRightToLeft() {
				//Redraws the magic menu
				int spellScreen = character.characterClass.getNextSpellBookLowToHigh(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
				if (spellScreen != -1)
					initSpells(spellScreen);
				else
					initBattleScreen();
			}

			@Override
			public void onLeftToRight() {
				//Redraws the magic menu
				int spellScreen = character.characterClass.getNextSpellBookHighToLow(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
				if (spellScreen != -1)
					initSpells(spellScreen);
				else
					initBattleScreen();
			}

			@Override
			public void onBottomToTop() {
			}
		});

		list.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				character.info.setGroupIsExpandedElement(groupPosition, 1);
			}
		});
		list.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
				character.info.setGroupIsExpandedElement(groupPosition, 0);
			}
		});
	}

	//This class handles the setting and viewing of the group and child elements of the expandable list
	public class SpellExpandableListAdapter extends BaseExpandableListAdapter {

		//The array containing the spells remaining 
		int[] groupArray;

		//Get the number of spell levels that character has achieved
		int numSpellLevels = character.characterClass.getMaxSpellLevelForSpecificClass(currentSpellBookView);

		//Used to calculate the total number of group array elements to draw
		int totalLevel;

		private String[] groupData;

		private String[][] childrenData;

		//Constructor FTW
		public SpellExpandableListAdapter() {
		}

		/**
		 * This wonderful function manages to pull all the character's spell
		 * information and turn it into ledgable, array structured, data that
		 * can be used for the display and interaction of said spells
		 */
		public void setSpellArrays() {

			//TODO: This is probably not needed
			inflater = (LayoutInflater) BattleScreen.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			numSpellLevels = character.characterClass.getMaxSpellLevelForSpecificClass(currentSpellBookView);
			groupArray = character.characterClass.getSpellCastsRemainingForSpecificClass(currentSpellBookView);

			//Iterate through 'dem spells!
			Iterator<String> iter;

			//Used to temporarily hold the spells for that level and store them into the array?
			ArrayList<String> spellsPerLevel = new ArrayList<String>();

			//get the spell list
			ArrayList<Spell> tempSpells = character.getSpells(currentSpellBookView);
			//for every spell level that the character has magic for...

			ArrayList<String> groupArrayListData = new ArrayList<String>();

			childrenData = new String[10][1];

			//Loop through each spell level for the character
			for (int i = 0; i < numSpellLevels; i++) {
				if (groupArray[i] == 1)
					groupArrayListData.add("Level " + i + ":" + groupArray[i] + " cast remaining");
				else
					groupArrayListData.add("Level " + i + ":" + groupArray[i] + " casts remaining");
				for (Spell s : tempSpells) {
					//Get all the 0 level spells and put their name in an ArrayList
					if (s.getSpellLevel(currentSpellBookView) == i) {
						//TODO: ADD implimentation for SORCERERS and other special magic users.
						// IF isAcative...
						spellsPerLevel.add(s.getName());
					}
				}
				//Make iterator to loop through the arrayList and turn it into a String[] array
				iter = spellsPerLevel.iterator();

				//Create a variable length array based on the number of spells in that spell level range
				String[] tempArray = new String[spellsPerLevel.size()];

				//For loop that does the assigning from ArrayList to String[]
				for (int j = 0; j < spellsPerLevel.size(); j++) {
					String item = iter.next();
					tempArray[j] = item;
				}
				//Assign the childData[] to tempArray
				childrenData[i] = tempArray;

				//Reset the ArrayList for use on the next go around
				spellsPerLevel.clear();
			}

			String[] groupStringArray = new String[groupArrayListData.size()];
			int count = 0;
			for (String s : groupArrayListData) {
				groupStringArray[count] = s;
				count++;
			}

			//Gets rid of excess slots
			for (int i = numSpellLevels; i < 10; i++) {
				childrenData[i] = null;
			}
			groupData = groupStringArray;
			spellListAdapter.notifyDataSetChanged();
		}

		//The temporary holder for the child view. Used for stuff like setting button listeners
		public class ChildViewHolder {
			LinearLayout spellChildLinear;
			TextView txtCharName;
			Button castButton;
		}

		//See above comment
		public class GroupViewHolder {
			TextView txtSpellLevel;
			TextView txtSpellRemaining;
		}

		//Gets the child data at the specified position
		public Object getChild(int groupPosition, int childPosition) {
			return childrenData[groupPosition][childPosition];
		}

		//Gets the id of the child
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		//Gets the number of children in the list
		public int getChildrenCount(int groupPosition) {
			return childrenData[groupPosition].length;
		}

		//This part actually is very useful.. Does stuff like Sets data pulled from the adapter, and sets onclickListeners
		//Also an onFling listener here incase there needs to be scrolling back to the main BattleScreen
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

			//Sets the groupPosition so it can be used by the clickListener
			final int groupPos = groupPosition;

			//I do not completely understand all of this...
			final ChildViewHolder holder;
			if (convertView == null) {
				//Sets up a new holder to temporaraly hold the listeners that will be assigned to the binded variables
				holder = new ChildViewHolder();
				convertView = inflater.inflate(R.layout.spell_child_row, null);

				//Find the IDs! Find them!!!!
				holder.spellChildLinear = (LinearLayout) convertView.findViewById(R.id.spellchildLinearLayout);
				holder.txtCharName = (TextView) convertView.findViewById(R.id.battlemagicSpellChildTxt);
				holder.castButton = (Button) convertView.findViewById(R.id.battlemagicSpellChildButton);

				//"Sets the tag associated with this view. A tag can be used
				//to mark a view in its hierarchy and does not have to be unique within the hierarchy."
				convertView.setTag(holder);
			} else {
				holder = (ChildViewHolder) convertView.getTag();
			}
			String spellName = childrenData[groupPosition][childPosition];

			holder.txtCharName.setText(spellName);
			holder.castButton.setText("CAST");

			//Disables the casting of the spell if out of casts for that group
			// Get the max number of spell levels it can look at
			int maxLevel = character.characterClass.getMaxSpellLevelForSpecificClass(currentSpellBookView);
			//Get the remaining casts for that character
			int[] castsRemaining = character.characterClass.getSpellCastsRemainingForSpecificClass(currentSpellBookView); // Check if the curent level is out (use parent group)
			boolean foundCastable = false;
			if (castsRemaining[groupPosition] <= 0) {

				//if the current one is out
				//check the higher levels if the current level can use the casts of an upper level
				for (int i = groupPosition; i <= maxLevel; i++) {
					if (castsRemaining[i] > 0) {
						foundCastable = true;
					}
				}
				//else (it can not find and usable upper level casts)
			} else
				foundCastable = true;

			if (foundCastable == false)
				holder.castButton.setEnabled(false);
			else
				holder.castButton.setEnabled(true);

			//Button listeners always good...
			holder.castButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (character.characterClass.getSpeficficCharacterClassMagicType(currentSpellBookView) == ConstantHelper.CHAR_TYPE_SPECIAL_MAGIC) {
						//Disables the casting of the spell if out of casts for that group
						// Get the max number of spell levels it can look at
						int maxLevel = character.characterClass.getMaxSpellLevelForSpecificClass(currentSpellBookView);
						//Get the remaining casts for that character
						int[] castsRemaining = character.characterClass.getSpellCastsRemainingForSpecificClass(currentSpellBookView);
						// Check if the curent level is out (use parent group)
						if (castsRemaining[groupPos] <= 0) {
							//boolean foundCastable = false;

							//if the current one is out
							//check the higher levels if the current level can use the casts of an upper level
							for (int i = groupPos; i <= maxLevel; i++) {
								if (castsRemaining[i] > 0) {
									character.characterClass.decrementSpellsRemaining(currentSpellBookView, i);
									Log.d("CHAR", "DELETE UPPER SPELL");
									break;
								}
							}
						} else {
							character.characterClass.decrementSpellsRemaining(currentSpellBookView, groupPos);
						}
						setSpellArrays();
						spellListAdapter.notifyDataSetChanged();
					} else { //Is normal magic that needs to disable the button when clicked
						character.characterClass.decrementSpellsRemaining(currentSpellBookView, groupPos);
					}
				}
			});

			//onFling listener for returning to the previous view if a group is expanded
			holder.spellChildLinear.setOnTouchListener(new OnFlingGestureListener() {

				@Override
				public void onTopToBottom() {
				}

				@Override
				public void onRightToLeft() {
					//Redraws the magic menu
					int spellScreen = character.characterClass.getNextSpellBookLowToHigh(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
					if (spellScreen != -1)
						initSpells(spellScreen);
					else
						//TODO: CHANGE TO ITEM SCREEN
						//TODO: RESET currentSpellBookView
						initBattleScreen();
				}

				@Override
				public void onLeftToRight() {
					//Redraws the magic menu
					int spellScreen = character.characterClass.getNextSpellBookHighToLow(currentSpellBookView, character.characterClass.getCharacterClassLevelsArray());
					if (spellScreen != -1)
						initSpells(spellScreen);
					else
						initBattleScreen();
				}

				@Override
				public void onBottomToTop() {
				}
			});

			holder.spellChildLinear.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Log.d("CHAR", "CLICKED SPELL FOR INFO");
					//Get spell information from files based off the name and level
				}
			});

			holder.spellChildLinear.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {

					AlertDialog.Builder dialog = new AlertDialog.Builder(BattleScreen.this);
					dialog.setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							String spellName = holder.txtCharName.getText().toString();
							character.removeSpellFromClass(spellName, currentSpellBookView);

							setSpellArrays();
							spellListAdapter.notifyDataSetChanged();
						}
					}).setNegativeButton("No", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}

					}).setTitle("Delete this Spell?");

					AlertDialog alert = dialog.create();
					alert.show();

					//					String spellName = holder.txtCharName.getText().toString();
					//					character.removeSpellFromClass(spellName, currentSpellBookView);

					//					setSpellArrays();
					//					spellListAdapter.notifyDataSetChanged();
					return false;
				}
			});

			return convertView;
		}

		public Object getGroup(int groupPosition) {
			return groupData[groupPosition];
		}

		public int getGroupCount() {
			return groupData.length;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

			GroupViewHolder holder;
			if (convertView == null) {
				holder = new GroupViewHolder();
				convertView = inflater.inflate(R.layout.spell_group_row, null);

				holder.txtSpellLevel = (TextView) convertView.findViewById(R.id.battlemagicSpellRowLevelTxt);
				holder.txtSpellRemaining = (TextView) convertView.findViewById(R.id.battlemagicSpellRowRemainingTxt);

				convertView.setTag(holder);
			} else {
				holder = (GroupViewHolder) convertView.getTag();
			}
			String groupInfo = groupData[groupPosition];

			String delimiter = "\\:";
			String[] split = groupInfo.split(delimiter);

			holder.txtSpellLevel.setText(split[0]);
			holder.txtSpellRemaining.setText(split[1]);

			return convertView;
		}

		public boolean hasStableIds() {
			return false;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}

	}
	//
	//
	//
	//End of magic_spells_menu code
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
	//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//-//
}
